package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanCustomerPlanBiz;
import com.egao.orderB.entity.OceanCustomerPlan;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-11-10 16:32:48
 * @version ：1.0
 */
@Repository
public class OceanCustomerPlanBizImpl extends BaseDao implements OceanCustomerPlanBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OceanCustomerPlan oceanCustomerPlan, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanCustomerPlan> rm = new BeanPropertyRowMapper<OceanCustomerPlan>(OceanCustomerPlan.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id,o.customer,o.pinyin");
    sql.append(",o.ship,o.vessel,o.voyage,o.air_route,o.go_ship_day");
    sql.append(",o.box_type,o.price");
		sql.append(",o.deposit,o.deposit_path,o.entrustment_annex,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_customer_plan o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where 1=1 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
		if (Utils.isNotEmpty(oceanCustomerPlan.getShip())) {
			sql.append(" and instr(o.ship,:ship)>0 ");
			map.put("ship", oceanCustomerPlan.getShip());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getBoxType())) {
			sql.append(" and o.box_type=:box_type ");
			map.put("box_type", oceanCustomerPlan.getBoxType());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getGoShipDay())) {
			sql.append(" and instr(o.go_ship_day,:go_ship_day)>0 ");
			map.put("go_ship_day", oceanCustomerPlan.getGoShipDay());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getCustomer())) {
			sql.append(" and (instr(o.customer,:customer)>0 or instr(o.pinyin,:customer)>0) ");
			map.put("customer", oceanCustomerPlan.getCustomer());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getDate1())) {
			sql.append(" and o.go_ship_day>= :date1 ");
			map.put("date1", oceanCustomerPlan.getDate1());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getDate2())) {
			sql.append(" and o.go_ship_day<=:date2 ");
			map.put("date2", oceanCustomerPlan.getDate2());
		}
		//deleted正常0   删除1  取消2
		if(Utils.isNotEmpty(oceanCustomerPlan.getDeleted()) && oceanCustomerPlan.getDeleted()==2){
			sql.append(" and o.deleted=2 ");
		} else {
			sql.append(" and o.deleted=0 ");
		}
		if(Utils.isNotEmpty(oceanCustomerPlan.getSort())&&Utils.isNotEmpty(oceanCustomerPlan.getOrder())){
			sql.append(" order by o."+oceanCustomerPlan.getSort()+" "+oceanCustomerPlan.getOrder());
		} else {
			sql.append(" order by o.create_time desc");
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OceanCustomerPlan> list(OceanCustomerPlan oceanCustomerPlan, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanCustomerPlan> rm = new BeanPropertyRowMapper<OceanCustomerPlan>(OceanCustomerPlan.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id,o.customer,o.pinyin");
		sql.append(",o.ship,o.vessel,o.voyage,o.air_route,o.go_ship_day");
		sql.append(",o.box_type,o.price");
		sql.append(",o.deposit,deposit_path,o.entrustment_annex,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_customer_plan o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
		if (Utils.isNotEmpty(oceanCustomerPlan.getShip())) {
			sql.append(" and instr(o.ship,:ship)>0 ");
			map.put("ship", oceanCustomerPlan.getShip());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getBoxType())) {
			sql.append(" and o.box_type=:box_type ");
			map.put("box_type", oceanCustomerPlan.getBoxType());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getGoShipDay())) {
			sql.append(" and instr(o.go_ship_day,:go_ship_day)>0 ");
			map.put("go_ship_day", oceanCustomerPlan.getGoShipDay());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getCustomer())) {
			sql.append(" and instr(o.customer,:customer)>0 ");
			map.put("customer", oceanCustomerPlan.getCustomer());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getDate1())) {
			sql.append(" and o.go_ship_day>= :date1 ");
			map.put("date1", oceanCustomerPlan.getDate1());
		}
		if (Utils.isNotEmpty(oceanCustomerPlan.getDate2())) {
			sql.append(" and o.go_ship_day<=:date2 ");
			map.put("date2", oceanCustomerPlan.getDate2());
		}
    if(Utils.isNotEmpty(oceanCustomerPlan)){
      if(Utils.isNotEmpty(oceanCustomerPlan.getSort())&&Utils.isNotEmpty(oceanCustomerPlan.getOrder())){
        sql.append(" order by o."+oceanCustomerPlan.getSort()+" "+oceanCustomerPlan.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OceanCustomerPlan getById(OceanCustomerPlan oceanCustomerPlan) {
		RowMapper<OceanCustomerPlan> rm = new BeanPropertyRowMapper<OceanCustomerPlan>(OceanCustomerPlan.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id,o.customer,o.pinyin");
		sql.append(",o.ship,o.vessel,o.voyage,o.air_route,o.go_ship_day");
		sql.append(",o.box_type,o.price");
		sql.append(",o.deposit,o.deposit_path,o.entrustment_annex,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_customer_plan o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.id=:id ");
		map.put("id", oceanCustomerPlan.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(OceanCustomerPlan oceanCustomerPlan, User loginUser) {
		oceanCustomerPlan.setPinyin(PinyinUtils.ToFirstChar(oceanCustomerPlan.getCustomer()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_customer_plan (");
    sql.append("organization_id,customer_id,customer,pinyin,ship,vessel");
		sql.append(",box_type,price");
		sql.append(",voyage,air_route,go_ship_day,deposit,deposit_path,entrustment_annex");
		sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:customer_id,:customer,:pinyin,:ship,:vessel");
		sql.append(",:box_type,:price");
		sql.append(",:voyage,:air_route,:go_ship_day,:deposit,:deposit_path,:entrustment_annex");
		sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("customer_id", oceanCustomerPlan.getCustomerId()==null?null:oceanCustomerPlan.getCustomerId()+"");
    map.put("customer", oceanCustomerPlan.getCustomer());
		map.put("pinyin", oceanCustomerPlan.getPinyin());
    map.put("ship", oceanCustomerPlan.getShip());
    map.put("vessel", oceanCustomerPlan.getVessel());
    map.put("voyage", oceanCustomerPlan.getVoyage());
    map.put("air_route", oceanCustomerPlan.getAirRoute());
    map.put("go_ship_day", oceanCustomerPlan.getGoShipDay());
    map.put("deposit", oceanCustomerPlan.getDeposit());
    map.put("deposit_path", oceanCustomerPlan.getDepositPath());
    map.put("entrustment_annex", oceanCustomerPlan.getEntrustmentAnnex());
    map.put("box_type", oceanCustomerPlan.getBoxType());
    map.put("price", oceanCustomerPlan.getPrice());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OceanCustomerPlan oceanCustomerPlan, User loginUser) {
		oceanCustomerPlan.setPinyin(PinyinUtils.ToFirstChar(oceanCustomerPlan.getCustomer()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_customer_plan set ");
    sql.append("customer_id=:customer_id,customer=:customer,pinyin=:pinyin,ship=:ship,vessel=:vessel ");
		sql.append(",voyage=:voyage,air_route=:air_route,go_ship_day=:go_ship_day,deposit=:deposit,deposit_path=:deposit_path ");
		sql.append(",box_type=:box_type,price=:price");
		sql.append(",entrustment_annex=:entrustment_annex,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
		map.put("customer_id", oceanCustomerPlan.getCustomerId()==null?null:oceanCustomerPlan.getCustomerId()+"");
    map.put("customer", oceanCustomerPlan.getCustomer());
    map.put("pinyin", oceanCustomerPlan.getPinyin());
    map.put("ship", oceanCustomerPlan.getShip());
    map.put("vessel", oceanCustomerPlan.getVessel());
    map.put("voyage", oceanCustomerPlan.getVoyage());
    map.put("air_route", oceanCustomerPlan.getAirRoute());
    map.put("go_ship_day", oceanCustomerPlan.getGoShipDay());
    map.put("deposit", oceanCustomerPlan.getDeposit());
    map.put("deposit_path", oceanCustomerPlan.getDepositPath());
    map.put("entrustment_annex", oceanCustomerPlan.getEntrustmentAnnex());
		map.put("box_type", oceanCustomerPlan.getBoxType());
		map.put("price", oceanCustomerPlan.getPrice());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanCustomerPlan.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(OceanCustomerPlan oceanCustomerPlan, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(oceanCustomerPlan.getId().length()>0){
			for(String ID : oceanCustomerPlan.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ocean_customer_plan set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
	@Override
	public boolean cancel(OceanCustomerPlan oceanCustomerPlan, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_customer_plan set deleted=2" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanCustomerPlan.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(),map);
		return count > 0;
	}
}
